﻿
        var provSel;
        var citySel;
        var distSel;
        $(function () {
            provSel = document.getElementById("_fm_d_0_pr");
            citySel = document.getElementById("_fm_d_0_c");
            distSel = document.getElementById("_fm_d_0_a");
            var cv = DistrictSelector.attach(provSel, citySel, distSel);
            $.post("/User/MyAddress", Address, "json");

            window['distSelector'] = cv;
        });
        function Address(data) {
            var dataList = data.list;
            var li = "";
            if (dataList != null && dataList.length > 0) {

                for (var i = 0; i < dataList.length; i++) {

                    var obj = dataList[i];
                    var id = obj.AID;
                    var Consignee = obj.AConsignee;
                    var Address = obj.Address;
                    var AreaCode = obj.AreaCode;
                    var Phone = obj.APhone;
                    var Tel = obj.ATel;
                    var ZipCode = obj.AZipCode;
                    var mobie = "";
                    var liclass = "";
                    if (Tel != "") {
                        mobie = Tel;
                    }
                    mobie = mobie == "" ? Phone : mobie + "/" + Phone;


                    if (obj.AISDefalut) {
                        $("#J_DeliverId").val(id);
                        $("#J_Addr_deliverName").val(Consignee);
                        $("#J_Addr_deliverCode").val(AreaCode);
                        $("#J_Addr_deliverMobile").val(mobie);
                        $("#J_Addr_deliverAddr").val(Address);
                        $("#J_Addr_postCode").val(ZipCode);

                        liclass = "J_Addr J_MakePoint selected defalut-addr";
                    } else {
                        liclass = "J_Addr J_MakePoint";
                    }
                    var pcd = distSelector.getpcd(AreaCode).split('-');
                    li += '<li id="J_Addr_' + id + '" onclick="changeAdd(this)" class="' + liclass + '" data-id="' + id + '" data-name="' + Consignee + '" data-deliverCode="' + AreaCode + '" data-addr="' + Address + '" data-post="' + ZipCode + '" data-mobi="' + Phone + '"';

                    li += '<div class="addr_hd">';
                    li += '<span class="addrinfo-province">' + pcd[0] + '</span> <span class="addrinfo-city" title="' + pcd[1] + '">' + pcd[1] + '</span>';
                    li += '<span class="addrinfo-name" title="' + Consignee + '">(' + Consignee + ' 收)</span>';
                    li += ' </div>';

                    li += '<div style="display: block;" class="addr_bd">';
                    li += '<span class="addrinfo-dist">' + pcd[2] + '</span> <span class="addrinfo-street" title="' + Address + '">';

                    li += '' + strsub(Address) + '</span> <span class="addrinfo-tel">' + mobie + ' </span>';
                    li += ' </div>';
                    if (obj.AISDefalut) {
                        li += '<div class="default-info"> 默认地址</div>';
                    }

                    li += '</li>';
                }
            }
            $("#address-list").html(li);
        }
        function changeAdd(obj) {
            $("#address-list li").removeClass("selected");
            $(obj).addClass("selected");
            $("#J_DeliverId").val($(obj).attr("data-id"));
            $("#J_Addr_deliverName").val($(obj).attr("data-name"));
            $("#J_Addr_deliverCode").val($(obj).attr("data-deliverCode"));
            $("#J_Addr_deliverMobile").val($(obj).attr("data-mobi"));
            $("#J_Addr_deliverAddr").val($(obj).attr("data-addr"));
            $("#J_Addr_postCode").val($(obj).attr("data-post"));
        }
        function strsub(str) {
            if (str.length > 15) {
                return str.substring(0, 15) + '...';

            }
            return str
        }
        var validate = function () {
            if (provSel.options.length > 1 && provSel.selectedIndex == 0)
                return false;
            if (citySel.options.length > 1 && citySel.selectedIndex == 0)
                return false;
            if (distSel.options.length > 1 && distSel.selectedIndex == 0)
                return false;
            return true;
        }
        function doChange(selObj) {
            var selValue = selObj.options[selObj.selectedIndex].value;
            var zip = document.getElementById("_fm_d_0_po");
            if (selValue == "810000") {
                zip.value = "999077";
                selectedGA = true;
            } else if (selValue == "820000") {
                zip.value = "999078";
                selectedGA = true;
            } else if (selectedGA) {
                zip.value = "";
                selectedGA = false;
            } else {
                selectedGA = false;
            }
        }
        function quanjiao2Banjiao(str) {
            if (str == "" || typeof (str) == "undefined") {
                return "";
            }
            var i;
            var result = '';

            for (i = 0; i < str.length; i++) {
                code = str.charCodeAt(i);
                if (code >= 65281 && code < 65373) {
                    result += String.fromCharCode(str.charCodeAt(i) - 65248);
                }
                else {
                    result += str.charAt(i);
                }
            }
            return result;
        }
        function createOrUpdate() {

            $("#mobilePhone").val(quanjiao2Banjiao($("#mobilePhone").val()));
            $("#divisionCode").val(quanjiao2Banjiao($("#divisionCode").val()));

            if ($("#addrName").val() == '') {
                alert('\u6536\u8D27\u4EBA\u59D3\u540D\u4E0D\u80FD\u4E3A\u7A7A');
                return false;
            }

            if (!validate()) {
                //地址必填弹出框
                alert('\u8bf7\u9009\u62e9\u6709\u6548\u7684\u7701\u5e02\u533a');
                return false;
            }

            var address = $("#addrMail").val();
            if (address == '' || typeof (address) == "undefined") {
                alert('\u8857\u9053\u5730\u5740\u4E0D\u80FD\u4E3A\u7A7A');
                return false;
            }
            if (address != '' && /^\d*$/.test(address)) {
                alert('\u8857\u9053\u5730\u5740\u4e0d\u80fd\u5168\u662f\u6570\u5b57');
                return false;
            }
            var codes = distSelector.getSelectedCodes();
            $("#divisionCode").val(codes[2]);

            PhoneCheck.easyInit();
            var isValidPhone = PhoneCheck.attachPhoneCheck(); //电话必填弹出框

            var phone = $("#phoneCode").val().trim();
            var mobilPhone = $("#mobilePhone").val().trim();
            var phonearea = $("#phoneSection").val().trim();
            isMobilPhone = /^1[3,4,5,8]\d{9}$/.test(mobilPhone);
            if (!isValidPhone) {

                return false;
            }
            if (mobilPhone && !isMobilPhone) {
                alert("手机格式不正确");
                return false;
            }



            $.post("/User/SaveAddress", $("#myAddress").serialize(), function (data) {

                if (data.ResultCode != "00") {
                    alert(data.ResultMsg);
                } else {
                    Address(data);

                }
            }, "json");
        }

        function CalTotal() {
            var pr = $("#order-table .J_ShopTotal");

            var total = 0;
            for (var i = 0; i < pr.length; i++) {

                total += parseFloat($(pr[i]).html());
            }
            $("#J_Total").html(total);
        }
